package com.own.demo.utils;

import org.geotools.geojson.feature.FeatureJSON;
import org.geotools.geojson.geom.GeometryJSON;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.simple.SimpleFeature;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
public class GeojsonUtil {

public static Geometry GeometryToObj(String geometryJson) {
    GeometryJSON gjson = new GeometryJSON();
    Reader reader = new StringReader(geometryJson);
    Geometry geometry = null;

    try {
        geometry = gjson.read(reader);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return geometry;
}

public static List<SimpleFeature> featuresToObj(List<String> featureStrs) {
    Reader reader = null;
    List<SimpleFeature> simpleFeatures = new ArrayList();

    for(String featureStr : featureStrs) {
        Reader var8 = new StringReader(featureStr);
        FeatureJSON featureJSON = new FeatureJSON();

        try {
            SimpleFeature simpleFeature = featureJSON.readFeature(var8);
            simpleFeatures.add(simpleFeature);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return simpleFeatures;
}

//public static List<Feature> featureStringToFeatures(List<String> featureStrs) {
//    GeometryFactory geometryFactory = new GeometryFactory();
//    List<Feature> features = new ArrayList();
//
//    for(String featureStr : featureStrs) {
//        Feature feature = GeoJsonFeatureConverter.fromGeoJsonFeature(featureStr, geometryFactory);
//        features.add(feature);
//    }
//
//    return features;
//}

public static String geoToJson(String wkt) {
    String json = null;

    try {
        WKTReader reader = new WKTReader();
        Geometry geometry = reader.read(wkt);
        StringWriter writer = new StringWriter();
        GeometryJSON g = new GeometryJSON();
        g.write(geometry, writer);
        json = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return json;
}
}